﻿// -----------------------------------------------------------------------
// <copyright file="SubscriptionRequestParser.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Zen.BlackBerry.Subscription.Web
{
	using System;
	using System.Web;
	using Zen.BlackBerry.Subscription.Auth;

	/// <summary>
	/// Parses the needed parameters out of various subscription-related HTTP
	/// requests.
	/// </summary>
	public static class SubscriptionRequestParser
	{
		/// <summary>
		/// Parses out the HTTP request parameters from a subscribe request and
		/// constructs a subscribe request with the following fields set: app id,
		/// address, OS version, model number, and type (the type field can be
		/// null if the app id does not correspond to a push application with a
		/// type of Public+Enterprise Push).
		/// </summary>
		/// <param name="request">The request.</param>
		/// <returns>
		/// A <see cref="SubscribeRequest"/> object.
		/// </returns>
		public static SubscribeRequest ParseSubscribeRequest(HttpRequest request)
		{
			SubscribeRequest result =
				new SubscribeRequest
				{
					PushApplicationId = request.QueryString["appid"].Trim(),
					Address = request.QueryString["address"].Trim(),
					OsVersion = request.QueryString["osversion"].Trim(),
					Model = request.QueryString["model"].Trim(),
				};
			string typeText = request.QueryString["type"].Trim();
			if (!string.IsNullOrEmpty(typeText))
			{
				SubscriberType type;
				if (Enum.TryParse<SubscriberType>(typeText, true, out type))
				{
					result.Type = type;
				}
			}
			return result;
		}

		/// <summary>
		/// Parses out the HTTP request parameters from an unsubscribe request
		/// and constructs an unsubscribe request with the app id field set.
		/// </summary>
		/// <param name="request">The request.</param>
		/// <returns>
		/// A <see cref="UnsubscribeRequest"/> object.
		/// </returns>
		public static UnsubscribeRequest ParseUnsubscribeRequest(
			HttpRequest request)
		{
			UnsubscribeRequest result =
				new UnsubscribeRequest
				{
					PushApplicationId = request.QueryString["appid"].Trim()
				};
			return result;
		}

		/// <summary>
		/// Parses out the HTTP request parameters from a suspend request and
		/// constructs a suspend request with the app id field set.
		/// </summary>
		/// <param name="request">The request.</param>
		/// <returns>
		/// A <see cref="SuspendRequest"/> object.
		/// </returns>
		public static SuspendRequest ParseSuspendRequest(HttpRequest request)
		{
			SuspendRequest result =
				new SuspendRequest
				{
					PushApplicationId = request.QueryString["appid"].Trim()
				};
			return result;
		}

		/// <summary>
		/// Parses out the HTTP request parameters from a resume request and
		/// constructs a resume request with the app id field set.
		/// </summary>
		/// <param name="request">The request.</param>
		/// <returns>
		/// A <see cref="ResumeRequest"/> object.
		/// </returns>
		public static ResumeRequest ParseResumeRequest(HttpRequest request)
		{
			ResumeRequest result =
				new ResumeRequest
				{
					PushApplicationId = request.QueryString["appid"].Trim()
				};
			return result;
		}

		/// <summary>
		/// Parses out the HTTP request parameters from an authentication
		/// request and constructs an authentication request with the app id,
		/// username and password fields set.
		/// </summary>
		/// <param name="request">The request.</param>
		/// <returns>
		/// A <see cref="AuthenticationRequest"/> object.
		/// </returns>
		public static AuthenticationRequest ParseAuthenticationRequest(
			HttpRequest request)
		{
			AuthenticationRequest result =
				new AuthenticationRequest
				{
					PushApplicationId = request.QueryString["appid"].Trim(),
					UserName = request.QueryString["username"].Trim(),
					Password = request.QueryString["password"].Trim(),
				};
			return result;
		}
	}
}
